package com.lance.answer.question_export;

import com.alibaba.fastjson.JSONObject;
import com.lance.answer.dto.Item;
import com.lance.answer.dto.QuestionAnswer;
import com.lance.answer.gui.answer.Util;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AnswerFix_Picture {

    /**
     * 	"questionCode":"7174AD2EF06A082270F135FA5298BF9D",
     * 		"questionTitle":"个体通过社会化得以（ ），获得发展的基点。",
     * @param args
     */
    public static void main(String[] args) throws IOException {
        fix("经济数学基础12");
    }

    public static void fix(String subjectName) throws IOException {
        subjectName = subjectName.trim();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");

        String answerStr1 = IOUtils.toString(inputStream,"utf-8");
        answerStr1 = Util.dealContent(answerStr1);

        List<QuestionAnswer> list = JSONObject.parseArray(answerStr1,QuestionAnswer.class);
        System.out.println(list.size());

        int i = 0;
        List<String> lines = new ArrayList<>();
        for (QuestionAnswer answerObject:list) {
            String questionTitle = Util.dealContent(answerObject.getQuestionTitle());
            String[] strs = questionTitle.split("\\|");
            if (strs.length == 2) {
               String url = strs[1];
               if (url.contains("http")) {
                  url = url.replace("[","");
                  url = url.replace("]","");
                  String[] urls = url.split(",");
                  questionTitle = strs[0] + "  ";
                  for (String u:urls) {
                      String pic = "!" + "[]" + "(" + u + ")" + "  ";
                      questionTitle = questionTitle + pic;
                  }
               }
            }
            lines.add(questionTitle + "<br>");

            for (Item item:answerObject.getItems()) {
               String ans = item.getContent();
               if (ans.contains("http")) {
                   ans = ans.replace("|","");
                   ans = ans.replace("[","(");
                   ans = ans.replace("]",")");
                   ans = item.getPrefix() + "  " + "!" + "[]" + ans + " " + "答案填这个：" + ans.substring(1,ans.length() -1);
               } else {
                   ans = item.getPrefix() + "  " + ans + " " + "答案填这个：" + ans;
               }
                lines.add(ans + "<br>");
            }

            lines.add("\n");

        }

        System.out.println(i);
        String path = "src/main/resources/" + subjectName + "/" + subjectName + "-answer.md";
        FileUtils.writeLines(new File(path),lines);
    }
}
